<template>
  <div class="component-upload-image">
    <el-upload
      :action="uploadImgUrl"
      list-type="picture-card"
      :before-upload="handleBeforeUpload"
      :on-success="handleUploadSuccess"
      :on-remove="handleRemove"
      :on-error="handleUploadError"
      :file-list="urls"
      :headers="headers"
      :limit="limit"
      name="file"
    >
    <i slot="default" class="el-icon-plus"/>
    <div slot="file" slot-scope="{file}">
      <el-image class="el-upload-list__item-thumbnail" :src="file.url" alt=""  fit="fill"/>
      <span class="el-upload-list__item-actions">
        <span title="预览" @click.stop="handlePictureCardPreview(file)">
          <i class="el-icon-zoom-in"></i>
        </span>
        <span title="移除" @click.stop="handleRemove(file)">
          <i class="el-icon-delete"></i>
        </span>
      </span>
    </div>
    </el-upload>

    <el-dialog :visible.sync="dialogVisible" width="600" append-to-body>
      <img :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto;">
    </el-dialog>

  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

export default {
  data() {
    return {
      dialogVisible: false,
      dialogImageUrl: '',
      disabled: false,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
    };
  },
  props: {
    value: {
      type: Array,
      default: () => []
    },
    // 存放el-upload所需格式[{url: 'http://hy.image.lixin123.cn/1622709342916.jpeg'},{url: 'http://hy.image.lixin123.cn/1622709342916.jpeg'}]
    urls: {
      type: Array,
      default: () => []
    },
    limit: {
      type: Number,
      default: 3
    }
  },
  methods: {
    handleRemove(file) {
      let urlsIndex = this.urls.indexOf(file);
      this.urls.splice(urlsIndex, 1);
      let valueIndex = this.value.indexOf(file.url);
      this.value.splice(valueIndex, 1);
      console.log("handleRemove");
      console.log(this.urls);
      console.log(this.value);
    },
    handleUploadSuccess(res) {
      let url = res.data.url;
      this.urls.push({'url': url})
      this.value.push(url)
      console.log("handleUploadSuccess");
      console.log(this.urls);
      console.log(this.value);

      this.$emit("input", this.value);
      this.loading.close();
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleBeforeUpload() {
      this.loading = this.$loading({
        lock: true,
        text: "上传中",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    handleUploadError() {
      this.$message({
        type: "error",
        message: "上传失败",
      });
      this.loading.close();
    },
  },
  watch: {},
};
</script>

<style scoped lang="scss">
.image {
  position: relative;
  .mask {
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    transition: all 0.3s;
  }
  &:hover .mask {
    opacity: 1;
  }
}
</style>
